//头部信息
app.controller('Header', function($scope, $http, $location, $timeout, common){
    $scope.user     = S('user_info');

    //数据加载
    $scope.load_data    = function(){
        if(!$scope.user){
            var url     = config.prefix_url+'/User/getInfo';
            $http.get(url).success(function(info){
                if(info.code != 1){
                    common.dealError(info);
                    return false;
                }
                $scope.user     = info.data.info;
                if($scope.user.nickname.length > 7){
                    $scope.user.nickname    = $scope.user.nickname.substr(0, 7)+'...';
                }
            });
        }
    }

    //退出登录
    $scope.logout   = function(){
        var url     = config.prefix_url+'/Public/logout';
        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
                return false;
            }
            window.open('./login.html', '_self');
        });
    }

    //更改密码
    $scope.re_pass  = function(){
        var url     = config.prefix_url+'/User/submitPassword';
        var param   = {
            'old_pass'   : $scope.old_pass,
            'new_pass'   : $scope.new_pass,
            're_new_pass': $scope.re_new_pass
        };
        
        $http.post(url, param).success(function(info){
            $('#modal-repass').modal('hide');
            if(info.code != 1){
                common.dealError(info);
                return false;
            }
            common.doAlert('密码修改成功!');
        });
    }

    //全屏
    $scope.full_screen  = function(){
        var WsShell = new ActiveXObject('WScript.Shell')
        WsShell.SendKeys('{F11}');
    }

    $scope.load_data();
});

//菜单栏导航控制
app.controller('Menu', function($scope, $http, $rootScope, $location, $timeout, common){

    //加载菜单
    common.loadMenu();

    $scope.collapse     = function(index){
        $('.s-list-group').collapse('hide');
        $('.s-list-group').eq(index).collapse('show');
    };

    $scope.open     = function(url){
        $('.s-list-group > .list-group-item').each(function(){
            if($(this).data('url') == url){
                $(this).addClass('active');
            }else{
                $(this).removeClass('active');
            }
        });

        common.openUrl(url);
    };
});

//用户列表
app.controller('UserIndex', function($scope, $http, $rootScope, $location, common){
    $scope.nickname = '';
    $scope.page     = '';
    $scope.nowPage  = 1;
    $scope.groups   = '';
    $scope.userId   = '';

    //加载数据
    $scope.load_users  = function(page){
        var url     = config.prefix_url+'/User/index?nickname='+$scope.nickname+'&p='+page;
        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
                return false;
            }

            $scope.users     = info.data.users;
            $scope.nickname  = info.data.search.nickname;
            $scope.pages     = info.data.pages;
            
            common.startPage($scope.load_users);
            common.startAllCheck();
        });
    }

    //更改状态
    $scope.change_status     = function(userId, method){
        var url   = config.prefix_url+'/User/changeStatus?method='+method+'&id='+userId;
        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
                return false;
            }
            $scope.load_users();
        });
    };
    //修改密码
    $scope.change_pwd_userid='';
    $scope.change_pwd = function(id){
        $scope.change_pwd_userid     = id;
        $('.modal-adedit-userPwd').modal();

    }
    //提交密码
    $scope.ad_new_pwd='';
    $scope.ad_q_pwd='';
    $scope.ad_changePwd   = function(){
        if($scope.ad_new_pwd==$scope.ad_q_pwd){
            var url   =config.prefix_url+ '/User/modifyPassword?id='+$scope.change_pwd_userid+'&password='+$scope.ad_q_pwd;

            $http.get(url).success(function(info){
                if(info.code != 1){
                    common.dealError(info);
                    return false;
                }
                $('.modal-adedit-userPwd').modal('hide');
                common.doAlert(info.data);
            });
        }else{
            alert('请填写相同密码!');
        }


    }
    //更改用户组
    $scope.change_groups    = function(userId){
        var url     = config.prefix_url+'/AuthManager/group?uid='+userId;
        $scope.userId   = userId;
        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
                return false;
            }

            var user_groups     = info.data.user_groups;
            $scope.groups   = info.data.auth_groups;
            
            $('.change-user-group').modal('show');
            $('.change-user-group').on('shown.bs.modal', function(){
                $(this).find('input[type="checkbox"]').prop('checked', false);
                for(var i = 0; i < user_groups.length; i++){
                    $(this).find('input[value='+user_groups[i]+']').prop('checked', true);
                }
            });
        });
    }

    //提交用户组
    $scope.save_groups  = function(){
        var url     = config.prefix_url+'/AuthManager/addToGroup?batch=1&uid='+$scope.userId+common.getSelectParam('group_id[]');
        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
                return false;
            }
            $('.change-user-group').modal('hide');
        });
    }

    //批量更改状态
    $scope.batch_change_status  = function(method){
        var param  = '';
        $('[name = "id[]"]').each(function(index){
            if($(this).prop('checked')){
                param  += '&id[]='+$(this).val();
            }
        });
        param   += '&method='+method;

        var url     = config.prefix_url+'/User/changeStatus?'+param;
        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
                return false;
            }
            $scope.load_users();
        })
    }

    //添加用户
    $scope.add_user     = function(){
        var url   = config.prefix_url+'/User/add?'
                +'username='+$scope.aUsername
                +'&password='+$scope.aPassword
                +'&repassword='+$scope.aRepassword
                +'&email='+$scope.aEmail;

        $http.get(url).success(function(info){
            $('.modal-add-user').modal('hide');
            if(info.code != 1){
                common.dealError(info);
                return false;
            }
            $scope.load_users(1);
        });
    }
    
    $scope.load_users(1);
});

//权限管理首页控制器
app.controller('AuthManagerIndex', function($scope, $http, $rootScope, $location, common){
    $scope.auths    = '';
    $scope.title    = '';
    $scope.description  = '';
    $scope.id   = '';
    $scope.group_id     = '';

    //加载权限组数据
    $scope.load_groups   = function(page){
        var url     = config.prefix_url+'/AuthManager/index?p='+page;
        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
                return false;
            }
            $scope.auths    = info.data.list;
            $scope.pages    = info.data.pages;

            common.startPage($scope.load_groups);
            common.startAllCheck();
        });
    }

    //更改状态
    $scope.change_status    = function(auth_id, method){
        var url     = config.prefix_url+'/AuthManager/changeStatus?';
        var param   = '&id='+auth_id+'&method='+method;

        $http.get(url+param).success(function(info){
            if(info.code != 1){
                common.dealError(info);
                return false;
            }
            $scope.load_groups();
        });
    }

    //批量更改状态
    $scope.batch_change_status  = function(method){
        var url   = config.prefix_url+'/AuthManager/changeStatus?method='+method;
        $('[name = "id[]"').each(function(index){
            if($(this).prop('checked')){
                url   += '&id[]='+$(this).val();
            }
        });
        
        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
                return false;
            }
            $scope.load_groups();
        });
    }

    //详情查看
    $scope.detail   = function(id){
        var url     = config.prefix_url+'/AuthManager/editGroup?id='+id;
        $http.get(url).success(function(info){
            $scope.title    = info.data.title;
            $scope.description  = info.data.description;
            $scope.id   = info.data.id;

            $('.modal-edit-group').modal('show');
        });
    }

    //保存更改
    $scope.save     = function(){
        var url     = config.prefix_url+'/AuthManager/writeGroup?id='+$scope.id+'&title='+$scope.title+'&description='
            +$scope.description;

        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
                return false;
            }
            $('.modal-edit-group').modal('hide');
            $scope.load_groups();
        });
    }

    //权限详情查看
    $scope.detail_group_auths   = function(id){
        $scope.group_id     = id;
        var url     = config.prefix_url+'/AuthManager/access?group_id='+id;

        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
                return false;
            }
            $scope.mode_list    = info.data.mode_list;
        
            var is_auths    = info.data.this_group.rules;
            var is_auths    = is_auths.split(',');

            $('.modal-group-auth').modal('show');
            $('.modal-group-auth').on('shown.bs.modal', function(){
                $('.modal-group-auth').find('[type="checkbox"]').prop('checked', false);

                for(var i in is_auths){
                    $('.modal-group-auth').find('[value = "'+is_auths[i]+'"]').prop('checked', true);
                }
            });
        });
    }

    $scope.auths_save   = function(){
        var param   = 'id='+$scope.group_id;
        $('.modal-group-auth').find('[type = "checkbox"]').each(function(index){
            if($(this).prop('checked')){
                param   += '&rules[]='+$(this).val();
            }
        });
        
        var url     = config.prefix_url+'/AuthManager/writeGroup?'+param;

        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
                return false;
            }
            $('.modal-group-auth').modal('hide');
        })
    }

    //添加组
    $scope.add_group    = function(){
        var url     = config.prefix_url+'/AuthManager/writeGroup?title='+$scope.aTitle+'&description='+$scope.aDescription;
        
        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
                return false;
            }
            $('.modal-add-group').modal('hide');
            $scope.load_groups(1);
        });
    }

    $scope.load_groups(1);
});

//菜单管理控制器
app.controller('MenuIndex', function($scope, $http, $rootScope, $location, common){
    $scope.menus    = '';
    $scope.title    = '';
    $scope.sort     = '';
    $scope.url      = '';
    $scope.group    = '';
    $scope.id   = '';
    $scope.pid  = '';
    $scope.option_menus     = '';

    $scope.load_menus   = function(){
        var url     = config.prefix_url+'/Menu/index';
        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
                return false;
            }
            $scope.menus    = info.data;
            setTimeout(function(){
                $('.list-recursive').find('.fa-sys').click(function(){
                    var tar     = $(this).closest('.list-group-item').next('.list-recursive.systole');
                    tar.collapse('toggle');
                    if($(this).hasClass('fa-angle-down')){
                        $(this).removeClass('fa-angle-down')
                            .addClass('fa-angle-up');
                    }else{
                        $(this).removeClass('fa-angle-up')
                            .addClass('fa-angle-down');
                    }
                });
            }, config.dom_delay);
        });
    }

    //编辑、添加菜单
    $scope.edit     = function(menu_id){
        //添加菜单
        if(menu_id == 0){
            $scope.title    = '';
            $scope.sort     = '';
            $scope.url  = '';
            $scope.group    = '';
            $scope.id   = '';
            $scope.pid  = '';
            $scope.tip = '';
            $scope.hide = 0;

            var url  = config.prefix_url+'/Menu/add';
            $http.get(url).success(function(info){
                if(info.code != 1){
                    common.dealError(info);
                    return false;
                }
                $scope.option_menus    = info.data;
                $('.modal-add-menu').modal('show');
                $('.modal-add-menu').on('shown.bs.modal', function(){
                    $('option[value = 0]').prop('selected', true);
                });
            });

            return 0;
        }

        var url     = config.prefix_url+'/Menu/edit?id='+menu_id;
        $scope.id   = menu_id;
        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
                return false;
            }
            $scope.title    = info.data.info.title;
            $scope.sort     = parseInt(info.data.info.sort);
            $scope.url      = info.data.info.url;
            $scope.group    = info.data.info.group;
            $scope.option_menus     = info.data.menus;
            $scope.pid  = info.data.info.pid;
            $scope.hide = info.data.info.hide;
            $scope.tip  = info.data.info.tip;

            var pid     = info.data.info.pid;

            $('.modal-edit-menu').modal('show');
            $('.modal-edit-menu').on('shown.bs.modal', function(){
                $('option').each(function(){
                    if($(this).val() == pid){
                        $(this).prop('selected', true);
                    }
                });
                $('.modal-edit-menu').find('.radio').each(function(index){
                    if($(this).val() == $scope.hide){
                        $(this).prop('checked', true);
                    }
                });
            });
        });
    }

    //保存编辑、添加
    $scope.save     = function(type){
        //保存添加
        if(type == 'add'){
            var param   = '&title=' + $scope.title;
            param   += '&sort=' + $scope.sort;
            param   += '&url='  + $scope.url;
            param   += '&group=' + $scope.group;
            param   += '&pid=' + $scope.pid;
            param   += '&hide='+ $scope.hide;
            param   += '&tip='+ $scope.tip;

            var url     = config.prefix_url+'/Menu/add?'+param;
            $http.get(url).success(function(info){
                if(info.code != 1){
                    common.dealError(info);
                    return false;
                }
                $('.modal-add-menu').modal('hide');
                $scope.load_menus();
                common.loadMenu();
            });
            return 0;
        }

        var param   = '&title=' + $scope.title;
        param   += '&sort=' + $scope.sort;
        param   += '&url='  + $scope.url;
        param   += '&group='+ $scope.group;
        param   += '&id='   + $scope.id;
        param   += '&pid='  + $scope.pid;
        param   += '&hide=' + $scope.hide;
        param   += '&tip='  + $scope.tip;

        var url     = config.prefix_url+'/Menu/edit?'+param;
        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
                return false;
            }

            $('.modal-edit-menu').modal('hide');
            $scope.load_menus();
            common.loadMenu();
        });
    }

    //删除菜单
    $scope.del      = function(menu_id){
        var url     = config.prefix_url+'/Menu/del?id='+menu_id;

        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
                return false;
            }
            
            $scope.load_menus();
            common.loadMenu();
        });
    }

    $scope.load_menus(); 
});

//园长提现列表控制器
app.controller('DirectorIndex', function($scope, $http, $rootScope, $location, common){
    $scope.load_js   = function(){
        //使用日期控件
        $('.date').datetimepicker({
            format: 'yyyy-mm-dd',
            language:  'zh-CN',
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            minView:2,
            viewSelect: 2,
            forceParse: 0,
            showMeridian: 1
        });
    }
    $scope.load_js();
    $scope.status='';
    $scope.startDate = '';  //开始时间
    $scope.endDate = '';    //结束时间
    //展示提现列表
    $scope.lists  = function(page){
        $('.nav-tabs li').removeClass('active');
        switch ($scope.status){
            case 2:
                $('.first').addClass('active');
                $scope.status = 2;
                break;
            case 1:
                $('.second').addClass('active');
                $scope.status = 1;
                break;
            case 0:
                $('.third').addClass('active');
                $scope.status = 0;
                break;
            default:
                $('.first').addClass('active');
                $scope.status = 2;
                break;
        }
        var url   =config.prefix_url +  '/Withdraw/Index?p='+page+'&status='+$scope.status+'&state=101'+'&starttime='+$scope.startDate+'&endtime='+$scope.endDate;

        $http.get(url).success(function(info){
            if(info.code == 1){
                $scope.applys     = info.data.applys;
                //设置分页事件
                $scope.pages    = info.data.pages;
                common.startPage($scope.lists);
                common.startAllCheck();
            }
            else {
                common.dealError(info);
            }
        });
    }
    //初始化列表
    $scope.lists();
    //根据状态查询，切换签页
    $scope.changeIndex = function (status) {
        $scope.status = status;
        $scope.lists();
    }
    //审核
    $scope.applyid ='';
    $scope.first_check= function(id){
        $scope.applyid ='&id='+id;
        $(".modal-first").modal();
    }
    $scope.first_change_status= function(){
        var param  = '';
        $('[name = "id[]"]').each(function(index){
            if($(this).prop('checked')){
                param  += '&id[]='+$(this).val();
            }
        });
        $scope.applyid =param;
        $(".modal-first").modal();
    }
    $scope.second_check= function(id){
        $scope.applyid ='&id='+id;
        $(".modal-second").modal();
    }
    $scope.second_change_status= function(){
        var param  = '';
        $('[name = "id[]"]').each(function(index){
            if($(this).prop('checked')){
                param  += '&id[]='+$(this).val();
            }
        });
        $scope.applyid =param;
        $(".modal-second").modal();
    }

    $scope.content='';
    $scope.first_change_staus   = function(st){
        var url   =config.prefix_url +  '/Withdraw/firstchangestaus?status='+st+$scope.applyid+'&remark='+$scope.content;
        $http.get(url).success(function(info){
            if(info.code == 1){
                $scope.lists();
            }else{
                common.dealError(info);
            }
        });
    }
    $scope.second_change_staus   = function(st){
        var url   =config.prefix_url +  '/Withdraw/secondchangestaus?status='+st+$scope.applyid+'&remark='+$scope.content;
        console.log(url);
        $http.get(url).success(function(info){
            /*console.log('aaaa');
            console.log(info);*/
            if(info.code == 1){
                $scope.lists();
            }else{
                common.dealError(info);
            }
        });
    }

    //数据导出
    $scope.out_list = function(){
        var url     = config.prefix_url +  '/Withdraw/outList?status='+$scope.status+'&state=101'+'&starttime='+$scope.startDate+'&endtime='+$scope.endDate;
        console.log(url);return false;
        window.open(url, '_self');
    }
});

//推广员提现列表控制器
app.controller('SellerIndex', function($scope, $http, $rootScope, $location, common){
    $scope.load_js   = function(){
        //使用日期控件
        $('.date').datetimepicker({
            format: 'yyyy-mm-dd',
            language:  'zh-CN',
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            minView:2,
            viewSelect: 2,
            forceParse: 0,
            showMeridian: 1
        });
    }
    $scope.load_js();
    $scope.status='';
    $scope.startDate = '';  //开始时间
    $scope.endDate = '';    //结束时间
    //展示提现列表
    $scope.lists  = function(page){
        $('.nav-tabs li').removeClass('active');
        switch ($scope.status){
            case 2:
                $('.first').addClass('active');
                $scope.status = 2;
                break;
            case 1:
                $('.second').addClass('active');
                $scope.status = 1;
                break;
            case 0:
                $('.third').addClass('active');
                $scope.status = 0;
                break;
            default:
                $('.first').addClass('active');
                $scope.status = 2;
                break;
        }
        var url   =config.prefix_url +  '/Withdraw/Index?p='+page+'&status='+$scope.status+'&state=102'+'&starttime='+$scope.startDate+'&endtime='+$scope.endDate;
        
        $http.get(url).success(function(info){
            if(info.code == 1){
                $scope.applys     = info.data.applys;
                //设置分页事件
                $scope.pages    = info.data.pages;
                common.startPage($scope.lists);
                common.startAllCheck();
            }
            else {
                common.dealError(info);
            }
        });
    }
    //初始化列表
    $scope.lists();
    //根据状态查询，切换签页
    $scope.changeIndex = function (status) {
        $scope.status = status;
        $scope.lists();
    }
    //审核
    $scope.applyid ='';
    $scope.first_check= function(id){
        $scope.applyid ='&id='+id;
        $(".modal-first").modal();
    }
    $scope.first_change_status= function(){
        var param  = '';
        $('[name = "id[]"]').each(function(index){
            if($(this).prop('checked')){
                param  += '&id[]='+$(this).val();
            }
        });
        $scope.applyid =param;
        $(".modal-first").modal();
    }
    $scope.second_check= function(id){
        $scope.applyid ='&id='+id;
        $(".modal-second").modal();
    }
    $scope.second_change_status= function(){
        var param  = '';
        $('[name = "id[]"]').each(function(index){
            if($(this).prop('checked')){
                param  += '&id[]='+$(this).val();
            }
        });
        $scope.applyid =param;
        $(".modal-second").modal();
    }

    $scope.content='';
    $scope.first_change_staus   = function(st){
        var url   =config.prefix_url +  '/Withdraw/firstchangestaus?status='+st+$scope.applyid+'&remark='+$scope.content;
        $http.get(url).success(function(info){
            if(info.code == 1){
                $scope.lists();
            }else{
                common.dealError(info);
            }
        });
    }
    $scope.second_change_staus   = function(st){
        var url   =config.prefix_url +  '/Withdraw/secondchangestaus?status='+st+$scope.applyid+'&remark='+$scope.content;
        $http.get(url).success(function(info){
            if(info.code == 1){
                $scope.lists();
            }else{
                common.dealError(info);
            }
        });
    }

    //数据导出
    $scope.out_list = function(){
        var url     = config.prefix_url +  '/Withdraw/outList?status='+$scope.status+'&state=102'+'&starttime='+$scope.startDate+'&endtime='+$scope.endDate;
        window.open(url, '_self');
    }
});

//税率调整控制器
app.controller('Tax', function($scope, $http, $rootScope, $location, common){
    //获取税率
    $scope.lists  = function(){
        var url   =config.prefix_url +  '/Withdraw/tax';
        
        $http.get(url).success(function(info){
            if(info.code == 1){
                $scope.tax     = info.data.tax;
            }
            else {
                common.dealError(info);
            }
        });
    }
    //初始化列表
    $scope.tax ='';
    $scope.lists();
    //修改税率
    $scope.save   = function(){
        var url   =config.prefix_url + '/Withdraw/adtax?tax='+$scope.tax;
        $http.get(url).success(function(info){
            if(info.code == 1){
                alert('修改成功');
            }else{
                common.dealError(info);
            }
        });
    }
});
